package util

import (
	"ops.alert.center.service/internal/middleware"
	"strings"
)

//根据应用找出订阅且发送方式包含企业微信的电话号码的集合
func FindphonesByapplication(application, alertcontent string, eventOccurTimestamp int64, ShouldTable string) (thisPhones []string, wechat_contact_ids []int, this_appi_id int) {
	//如果企业微信的发送方式暂停了直接return
	for _, Alertways := range middleware.ThisAlertways {
		if strings.Contains(Alertways.Name, "微信") && Alertways.Pause {
			return
		}
	}
	for _, zhaoapplication := range middleware.ThisZhaoapplication {
		if zhaoapplication.Name == application {
			this_appi_id = zhaoapplication.App_id
		}
	}
	if this_appi_id == 0 {
		return
	}
	for _, Alertsubscriptions := range middleware.ThisAlertsubscriptions {
		blackSlice := strings.Split(Alertsubscriptions.Black_words, ";")
		if (Alertsubscriptions.App_id == this_appi_id) && strings.Contains(Alertsubscriptions.Way_ids, "4") && !ContainString(blackSlice, alertcontent) {
			for _, Alertcontact := range middleware.ThisAlertcontact {
				if Alertcontact.Contact_id == Alertsubscriptions.Contact_id {
					//查询告警次数限制，超过就pass
					count := SearchAlertHistoryimes(Alertcontact.Contact_id, this_appi_id, eventOccurTimestamp, alertcontent, ShouldTable)
					if Alertsubscriptions.Count >= count {
						thisPhones = append(thisPhones, Alertcontact.Phone)
						wechat_contact_ids = append(wechat_contact_ids, Alertcontact.Contact_id)
					}
				}
			}
		}
	}
	return
}
